package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qf.entity.Goods;
import com.qf.service.ISearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 刘亚康
 * @Date 2020/4/4
 * @Version 1.0'
 * @Description
 */
@Slf4j
@Service
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private SolrClient solrClient;

    /**
     * 根据用户搜索的key返回商品集合
     * @param key
     * @return
     */
    @Override
    public List<Goods> getGoodsListByKey(String key) {
        SolrQuery query=new SolrQuery();
        log.info("需要查询的商品关键字为："+key);
        if (!StringUtils.isEmpty(key)){
            log.info("key不为空");
           query.setQuery(String.format("goodsName:%s || goodsDesc:%s",key,key));
            //设置代码高亮
            query.setHighlight(true);
            //设置高亮前缀后缀
            query.setHighlightSimplePre("<font color='red'>");
            query.setHighlightSimplePost("</font>");
            //设置高亮字段
            query.addHighlightField("goodsName");
            query.addHighlightField("goodsDesc");
        }else {
            log.info("key为空");
           query.setQuery(String.format("goodsName:* || goodsDesc:*"));
        }
        QueryResponse queryResponse=null;
        try {
            queryResponse = solrClient.query(query);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //查询含有高亮的集合
        //map<goodsId,Map<goodsName,List<String>>>
        //Map<有结果集的id，<有结果集的字段，【值1】【值2】>>
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
        //查询所有匹配到的集合
        SolrDocumentList results = queryResponse.getResults();
        List<Goods> goodsList=new ArrayList<>();
        for (SolrDocument result : results) {
            Goods goods = new Goods();
            String id = result.get("id").toString();
            goods.setGoodsId(Integer.parseInt(id));
            goods.setGoodsName(result.get("goodsName").toString());
            goods.setGoodsDesc(result.get("goodsDesc").toString());
            goods.setGoodsPic(result.get("goodsPic").toString());
            goods.setGoodsPrice(BigDecimal.valueOf(Double.valueOf(result.get("goodsPrice").toString())));
            //判断高亮集合是否为空，如果key为空，高亮集合没有字段就为空
            if (highlighting!=null) {
                //判断当前商品中是否存在高亮
                Map<String,List<String>> map=highlighting.get(id);
                if (map!=null){
                    //如果存在高亮，替换goodsName
                    List<String> goodsNameList = map.get("goodsName");
                    if (goodsNameList!=null){
                        goods.setGoodsName(goodsNameList.get(0));
                    }
                    List<String> goodsDescList = map.get("goodsDesc");
                    if (goodsDescList!=null) {
                        goods.setGoodsDesc(goodsDescList.get(0));
                    }
                }
            }

            goodsList.add(goods);
        }
        return goodsList;
    }

    /**
     * 往solr中添加索引
     *
     * @param goods
     */
    @Override
    public void addGoodsToSolr(Goods goods) {
        SolrInputDocument document=new SolrInputDocument();
        document.addField("goodsName",goods.getGoodsName());
        document.setField("goodsDesc",goods.getGoodsDesc());
        document.setField("id",goods.getGoodsId());
        document.setField("goodsPic",goods.getGoodsPic());
        document.setField("goodsPrice",goods.getGoodsPrice().toString());

        try {
            solrClient.add(document);
            solrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
